package com.uphone.kingmall.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.base.BaseBean;
import com.uphone.kingmall.bean.MachCaptchaBean;
import com.uphone.kingmall.listener.onNormalRequestListener;

import java.util.UUID;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/19
 *     desc   :
 * </pre>
 */
public class DeviceUtil {
    public static void sendSmsCode(final Activity context, final String phone, final String type, final DeviceCodeCallBack callBack) {
        getDeviceCode(context, new DeviceUtil.DeviceCodeCallBack() {
            @Override
            public void callBack(final String deviceCode) {

                String machCaptcha = SharedPreferencesHelper.getMachCaptcha();
                if (TextUtils.isEmpty(machCaptcha)) {
                    HttpParams params = new HttpParams();
                    params.put("machCode", deviceCode);
                    OkGoUtils.normalRequest(MyUrl.getMachCaptcha, params, new onNormalRequestListener() {
                        @Override
                        public void onSuccess(String response, int code) {
                            MachCaptchaBean bean = GsonUtils.getGson().fromJson(response, MachCaptchaBean.class);
                            if (bean.getCode() == 0 && !TextUtils.isEmpty(bean.getMachCaptcha())) {
                                SharedPreferencesHelper.saveMachCaptcha(bean.getMachCaptcha());
                                //获取验证码
                                getPhoneCode(context, bean.getMachCaptcha(), deviceCode, phone, type, callBack);
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {

                        }
                    });
                } else {
                    //获取验证码
                    getPhoneCode(context, machCaptcha, deviceCode, phone, type, callBack);
                }

            }
        });

    }

    private static void getPhoneCode(final Activity activity, String machCaptcha, String deviceCode, final String phone, final String type, final DeviceCodeCallBack callBack) {
        if (callBack != null) {
            callBack.callBack(ConstansUtils.SUCCESS);
        }


        HttpParams params = new HttpParams();
        params.put("machCode", deviceCode);
        params.put("machCaptcha", machCaptcha);
        params.put("telephone", phone);
        params.put("type", type);

        OkGoUtils.progressRequest(MyUrl.sendSmsCode, activity, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String response, int code) {

                BaseBean bean = GsonUtils.getGson().fromJson(response, BaseBean.class);
                if (bean.getCode() == 1) {
                    ToastUtils.showShortToast(activity, "已发送验证码，请注意查收！");
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    /**
     * 获取手机设备码
     *
     * @param context
     * @param callBack
     */
    public static void getDeviceCode(final Activity context, final DeviceCodeCallBack callBack) {
        PermissionsUtils.getInstance().checkPermissions(context, new PermissionsUtils.IPermissionsResult() {
            @SuppressLint("MissingPermission")
            @Override
            public void passPermissions() {

                final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                final String tmDevice, tmSerial, tmPhone, androidId;
                tmDevice = "" + tm.getDeviceId();
                tmSerial = "" + tm.getSimSerialNumber();
                androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
                UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
                String uniqueId = deviceUuid.toString();
                callBack.callBack(uniqueId);
            }

            @Override
            public void refusePermissions() {

            }
        }, Manifest.permission.READ_PHONE_STATE);
    }

    public interface DeviceCodeCallBack {
        void callBack(String deviceCode);
    }
}
